package com.Jano.servlet;

import com.Jano.dto.CategoryDto;
import com.Jano.dto.Result;
import com.Jano.service.CategoryService;
import com.Jano.service.Impl.CategoryServiceImpl;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import static com.Jano.constant.BusinessConstant.*;

/**
 * description: CategoryServlet <br>
 * date: 2022/6/11 12:26 AM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
@WebServlet(urlPatterns = "/apis/categories")
public class CategoryServlet extends HttpServlet {
    CategoryService categoryService = new CategoryServiceImpl();
    /**
     * 使用Redis完成电商系统中首页分类信息的数据缓存
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("hello");
        List<CategoryDto> categoriesByRedis = categoryService.getCategoriesByRedis();
        resp.setContentType(MEDIA_TYPE_JSON);
        PrintWriter pw = resp.getWriter();
        Result result = new Result();
        result.setData(categoriesByRedis);
        result.setSuccess(true);
        pw.print(JSON.toJSONString(result,true));
    }
}
